package com.baizhi.controller;

import com.baizhi.entity.Article;
import com.baizhi.service.ArticleService;
import com.baizhi.service.LuceneService;
import org.apache.lucene.queryparser.classic.ParseException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * Created by 野马 on 2019/6/21.
 */
@Controller
@RequestMapping("/article")
public class ArticleController {

    private Logger logger = LoggerFactory.getLogger(ArticleController.class);

    @Autowired
    private ArticleService articleService;

    @Autowired
    private LuceneService luceneService;

    @RequestMapping("/selectArticle")
    @ResponseBody
    public Map selectArticle(String articleName,int page,int rows,String ArticleJS) throws Exception{

        logger.info(ArticleJS+"ArticleController的参数");

        if(ArticleJS != null && !"".equals(ArticleJS)){
            Map map = new HashMap();
            List<Article> articleList = luceneService.luceneSeleteByKeyword(ArticleJS);
            logger.info("ArticleController的返回值",articleList);
            int count = articleList.size();
            map.put("total",count);
            map.put("rows",articleList);
            return map;

        }else {
            return articleService.selectArticle(articleName, page, rows);
        }
    }

    //重置索引库
    @RequestMapping("/reset")
    @ResponseBody
    public Map reset() throws IOException {
        Map map = new HashMap();
        boolean b = luceneService.deleteAllIndex();
        if(b){
            boolean b1 = luceneService.addOneDocument(null);
            if (b1){
                map.put("reset",true);
            }else {
                map.put("reset",false);
            }
        }else {
            map.put("reset",false);
        }

        return map;

    }

















}
